package com.xkh.demo3.config;

import com.google.common.collect.Range;
import com.xkh.demo3.mapper.SystemLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.RangeShardingValue;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 分表策略
 * PreciseShardingAlgorithm接口用于实现=查询
 * RangeShardingAlgorithm用于实现>,<,>=,<=查询
 */
@Slf4j
public class ShardingAlgorithm implements PreciseShardingAlgorithm<Date>, RangeShardingAlgorithm<Date> {


    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        log.info("PreciseShardingAlgorithm接口用于实现=查询");
        StringBuilder tableName = new StringBuilder();
        tableName.append(preciseShardingValue.getLogicTableName()).append("_").append(formatDate(preciseShardingValue.getValue()));
        log.info("打印PreciseShardingAlgorithm接口的值 {}", tableName);
        return tableName.toString();
    }

    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames, RangeShardingValue<Date> rangeShardingValue) {
        log.info("RangeShardingAlgorithm用于实现>,<,>=,<=查询");
        //返回数据库节点名称的List
        Object[] table = availableTargetNames.toArray();
        Range valueRange = rangeShardingValue.getValueRange();
        //获取查询条件中范围值
        return splitDateList(table[0].toString(), date(valueRange.lowerEndpoint().toString()), date(valueRange.upperEndpoint().toString()));
    }

    // 日期转换成字符串
    public static String formatDate(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String str = format.format(date);
        return str;
    }


    //获取两个日期之间的所有日期集合
    public List<String> splitDateList(String tableName, Date startDate, Date endDate) {
        List<String> listDate = new ArrayList<>();
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            while (calendar.getTime().before(endDate) || calendar.getTime().equals(endDate)) {
                String name = tableName + "_" + dateFormat.format(calendar.getTime());
                listDate.add(name);
                calendar.add(Calendar.DAY_OF_MONTH, 1);
            }
            return listDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listDate;
    }

    public static Date date(String str) {

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = format.parse(str);
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }

        return date;
    }
}
